package com.XZDP;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.XZDP.entity.Shop;
import com.XZDP.mapper.ShopMapper;
import com.XZDP.service.IShopService;
import com.XZDP.utils.CacheUtils;
import com.XZDP.utils.Constant.RedisConstant;
import com.XZDP.utils.MyUtils;
import com.XZDP.utils.RedisID;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SpringBootTest
class chushihua extends ServiceImpl<ShopMapper, Shop> {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MyUtils myUtils;
    @Autowired
    private CacheUtils cacheUtils;
    @Autowired
    private RedisID redisID;
    @Autowired
    private IShopService shopService;

    /**
     * 初始化店铺缓存,测试逻辑过期
     */
    @Test
    public void saveredis() throws JsonProcessingException {
        List<Shop> list = list();
        for (int i = 0; i < list.size(); i++) {
            cacheUtils.SaveToRedisWithLogicalExpiration("shop:" + list.get(i).getId(), list.get(i), (long) (30 * 60 * 1000));
        }
    }

    @Test
    public void get() {
        final LocalDateTime localDateTime = LocalDateTime.now();
        long second = localDateTime.toEpochSecond(ZoneOffset.UTC);
        System.out.println(second);

    }

    /**
     * 初始化商铺GEO数据
     */
    @Test
    public void ShopGEO() {
        //从数据库查询出商铺信息
        List<Shop> list = shopService.list();
        //根据商铺类型进行分组
        //构造MAP集合存储分组id和商户信息
        Map<Long, List<Shop>> shoplist = new HashMap<>();
        //使用stream流分组存入MAP
        shoplist = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));
        shoplist.forEach((shopID, Shoplist) -> {
            String key = RedisConstant.SHOP_BEO_KEY + shopID;
            for (Shop shop : Shoplist) {
                redisTemplate.opsForGeo().add(key, new Point(shop.getX(), shop.getY()), shop.getId().toString());
            }
        });
    }
}
